/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package protocol;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.SocketException;
import java.security.NoSuchAlgorithmException;
import utility.DemoMD5;

/**
 *
 * @author masphei
 */
public class Message {
    
    /**
     * Format:10
     */
    public static final int MESSAGE_KEEP_ALIVE = 10;
    
    /**
     * Format :11 md5_username md5_password
     */
    public static final int MESSAGE_LOGIN = 11;
    
    /**
     * Format :12 
     */
    public static final int MESSAGE_METADATA = 12;
    
    
    /**
     * Format :13 filepath+binary
     */
    public static final int MESSAGE_METADATA_RESP = 13;
    
    
    /**
     * Format :14 filepath
     */
    public static final int MESSAGE_UPLOAD = 14;
    
    /**
     * Format :15 request download
     */
    public static final int MESSAGE_DOWNLOAD_REQUEST = 15;
    
    /**
     * Format 16
     */
    public static final int MESSAGE_ACK = 16;
    
    /**
     * Format 17
     */
    public static final int MESSAGE_NACK = 17;
    
    /**
     * Format 18
     */
    public static final int MESSAGE_LOGOUT = 18;
    
    /**
     * Format 19
     */
    public static final int MESSAGE_DELETE = 9;

    private StringBuilder message;
    
    public Message(int messageType,Object[] Parameter) throws UnsupportedEncodingException, NoSuchAlgorithmException{
        message = new StringBuilder();
        
        switch (messageType) {
            case MESSAGE_LOGIN :
                createLoginMessage((String)Parameter[0], (String)Parameter[1]);
                break;
            case MESSAGE_METADATA :
                createRequestMetadata();
                break;
            case MESSAGE_DOWNLOAD_REQUEST :
                createDownloadRequestMessage((String)Parameter[0], (String)Parameter[1]);
                break;
            case MESSAGE_UPLOAD :
                createUploadMessage((String)Parameter[0], (String)Parameter[1], (String)Parameter[2]);
                break;
            case MESSAGE_ACK :
                createAck();
                break;
            case MESSAGE_NACK :
                createNack();
                break;
            case MESSAGE_LOGOUT :
                createLogOut();
                break;
            case MESSAGE_DELETE :
                createDelete();
                break;
        }
    }
    
    public void createAck(){
        message.append(MESSAGE_ACK);
//        System.out.println("Kirim ack : " + message);
    }
    
    public void createNack(){
        message.append(MESSAGE_NACK);
    }
    
    public void createLogOut(){
        message.append((char)MESSAGE_LOGOUT);
    }
    
    public void createDelete(){
        message.append((char)MESSAGE_DELETE);
    }
    
    /**
     * Membuat message untuk login dengan username dan password
     * @param username
     * @param password 
     */
    public void createLoginMessage(String username,String password) throws UnsupportedEncodingException, NoSuchAlgorithmException{
        message.append((char)MESSAGE_LOGIN);
        message.append(' ');
        message.append(DemoMD5.MD5(username));
        message.append(' ');
        message.append(DemoMD5.MD5(password));
//        System.out.println("username : " + username);
//        System.out.println("password : " + password);
//        System.out.println("Message : " + message);
    }
    
    public void createRequestMetadata(){
        message.append((char)MESSAGE_METADATA);
    }
    public void createDownloadRequestMessage(String length, String pathFile) throws UnsupportedEncodingException, NoSuchAlgorithmException{
        message.append((char)MESSAGE_DOWNLOAD_REQUEST);
        
        message.append(' ');
        message.append(length);
        message.append(' ');
        message.append(pathFile);
    }
    
    
    public void createUploadMessage(String length, String type, String pathFile) throws UnsupportedEncodingException, NoSuchAlgorithmException{
        message.append((char)MESSAGE_UPLOAD);
        message.append(' ');
        message.append(length); // file size
        message.append(' ');
        message.append(type); // file type
        message.append(' ');
        message.append(pathFile); // file path
    }
    
    
    /**
     * Mengirim message
     * @return 
     */
    public byte[] toSend(){
        String msg = message.toString();
        
        byte[] retval = new byte[msg.length()];
        
        for(int i=0;i<msg.length();++i){
            retval[i] = (byte) msg.charAt(i);
        }
        return retval;
    }
    
//    public static Object[] readMessage(byte[] message){
//        Object[] retval = null;
//        
//        StringBuilder sb = new StringBuilder();
//        
//        //Case login
//        retval = new Object[1];
//        int i;
//        for(i=0;i<message.length;++i){
//            sb.append((char)message[i]);
//            retval[0] = sb;
//        }
//        
//        return retval;
//    }
    
    public static boolean sendMessage(Socket socket, BufferedOutputStream bos, Message message) throws IOException{
        
        byte[] send = message.toSend();
//        System.out.println("Send : " + byteToString(send));
        try {
            bos.write(send);
            bos.flush();
        } catch (SocketException e) {
            return false;
        }
        return true;
    }
    
    public static String byteToString(byte[] b){
        String retval = null;
        StringBuilder sb = new StringBuilder();
        int i;
        for(i=0;i<b.length;++i){
            sb.append((char)b[i]);
        }
        retval = sb.toString();
        return retval;
    }
    
    public byte[] stringToByte(String value){
        String msg = value.toString();
        
        byte[] retval = new byte[msg.length()];
        
        for(int i=0;i<msg.length();++i){
            retval[i] = (byte) msg.charAt(i);
        }
        return retval;
    }
}
